#include "Line.h"
#include "GlobalVariables.h"

Line::Line()
	:star::Object()
	,m_LeftBorder(0)
	,m_RightBorder(130)
	,m_LineWidth(5)
	,m_MoveSpeed(20.0f)
	,m_LineCounter(0)
{
	star::SpriteAnimationManager::GetInstance()->AddSpritesheet(_T("Tiles/LineAnimation.xml"));

	for(int i=0; i<25; ++i)
	{
		auto comp = new star::SpritesheetComponent(_T("Tiles/Line1.png"),_T("LineAnim"),_T("Line"));
		auto obj = new star::Object();
		obj->AddComponent(comp);
		AddChild(obj);
		m_LineParts.push_back(obj);
		obj->GetTransform()->Translate(0,float(i*5));

	}
	GetTransform()->TranslateX(float(m_LeftBorder));
	m_MoveSpeed=GlobalVariables::GetInstance()->GetLineSpeed();
	m_OrigSpeed = m_MoveSpeed;
}

void Line::Update( const star::Context& context )
{
	vec2 pos = GetTransform()->GetLocalPosition().pos2D();

	if(m_LineCounter>=10)
	{
		m_LineCounter=0;
		m_MoveSpeed+=3.0f;
		GlobalVariables::GetInstance()->SetLineSpeed(m_MoveSpeed);
	}

	if(pos.x<m_RightBorder)
	{
		pos.x+= m_MoveSpeed*float(context.mTimeManager->GetSeconds());
		GetTransform()->TranslateX(pos.x);
	}
	if(pos.x>m_RightBorder)
	{
		++m_LineCounter;
		GetTransform()->TranslateX(float(m_LeftBorder));
	}

}


void Line::AfterInitialzed()
{
	if(m_LineParts.size()!=0)
	m_LineWidth = m_LineParts[0]->GetComponent<star::SpritesheetComponent>()->GetWidth();
}


void Line::SetBorder( int leftborder, int rightborder )
{
	m_LeftBorder=leftborder;
	m_RightBorder=rightborder;
	m_MoveSpeed = m_OrigSpeed;
	GlobalVariables::GetInstance()->SetLineSpeed(m_MoveSpeed);
	GetTransform()->TranslateX(float(m_LeftBorder));
}
